﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DevComponents.DotNetBar;
using SpeechLib;
using Microsoft.Win32;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Xml;

namespace QLDBNH_CLIENT
{
    public partial class frmDangNhap : Form
    {
        Sent_Receive sr = new Sent_Receive();
        chuoi Chuoi = new chuoi();

        public static frmDangNhap mydangnhap;
        
        DataSet dn = new DataSet();

        DataTable dangnhap = new DataTable("dangnhap");

        public frmDangNhap()
        {
            InitializeComponent();

            mydangnhap = this;

            int day = DateTime.Now.Day;
            int month = DateTime.Now.Month;
            int year = DateTime.Now.Year;
            string dmy = "";

            dmy = day.ToString() + "/" + month + "/" + year;
            lb_dmy.Text = dmy;
        }

        private void bt_Dangnhap_Click(object sender, EventArgs e)
        {
            //khai bao bien va gan gia tri.
            string tendn = "", matkhau = "";

            //Get host name.
            string myHost = System.Net.Dns.GetHostName();

            //Get the IP from the host name.
            string myIP = System.Net.Dns.GetHostByName(myHost).AddressList[0].ToString();

            tendn = txt_Dangnhap.Text;
            matkhau = txt_Matkhau.Text;

            try
            {
                //ghi vao table 'dangnhap'.
                DataRow dn2 = dn.Tables["dangnhap"].NewRow();
                dn2[0] = "1";
                dn.Tables["dangnhap"].Rows.Add(dn2);

                //ghi vao table 'dangnhap'.
                DataRow dn1 = dn.Tables["dangnhap"].NewRow();
                dn1[0] = myHost;
                dn1[1] = myIP;
                dn1[2] = tendn;
                dn1[3] = matkhau;
                dn.Tables["dangnhap"].Rows.Add(dn1);

                //table dangnhap chuyen sang file dangnhap.txt.
                dn.Tables["dangnhap"].WriteXml("dangnhap.txt");

                //xoa table 'dangnhap'.
                dn.Tables["dangnhap"].Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            try
            {
                //Chuyen file dangnhap.txt sang chuoi.
                string dnhap = "";

                StreamReader r = new StreamReader("dangnhap.txt");
                while (r.Peek() != -1)
                {
                    dnhap = dnhap + Chuoi.c_h_string(r.ReadLine());
                }
                r.Close();

                //gui den may Server.
                sr.Sent(frmMainForm.ipserver, frmMainForm.portserver, dnhap);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void dangnhap_Load(object sender, EventArgs e)
        {
            //table 'dangnhap';
            dangnhap.Columns.Add(new DataColumn("IpClient", typeof(System.String)));
            dangnhap.Columns.Add(new DataColumn("NameClient", typeof(System.String)));
            dangnhap.Columns.Add(new DataColumn("Username", typeof(System.String)));
            dangnhap.Columns.Add(new DataColumn("Password", typeof(System.String)));
            dn.Tables.Add(dangnhap);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
